package com.cisco.pso;

import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.cisco.pso.PsoFileMatcher.MatchResult;

/**
 * main program.
 * 
 * It will scan the projects(2 bases, one pso), ouput will be detail result for
 * each pso file/pieces, merge script,...
 * 
 * @author jackding
 * 
 */
public class Analyzer {
	public static void main(String[] args) {

		String site = "chemical";
		String siteAbbreviation = "che";

		String baseLeftPath = "base/T27L10NSP25/";
		String baseRightPath = "base/T28L10NSP8/";

		String psoLeftPath = "pso/T27LC/";
		String psoRightPath = "pso/T28L/";

		String baseFileList = "base_old_file_list";
		String baseDiffFile = "base_diff_file_list";

		// String psoDiffFile = "pso_chemical_" + module + "_diff";
		String psoFileList = "pso_" + site + "_file_list";

		String reviseFile = null;
		reviseFile = site + "-revise";

		InputStreamReader baseFileListInput = new InputStreamReader(
				Merger.class.getClassLoader().getResourceAsStream(baseFileList));

		InputStreamReader baseDiffInput = new InputStreamReader(Merger.class
				.getClassLoader().getResourceAsStream(baseDiffFile));

		InputStreamReader psoInput = new InputStreamReader(Merger.class
				.getClassLoader().getResourceAsStream(psoFileList));

		FileListReader baseLR = new FileListReader(baseFileListInput);
		DiffReader baseDiffResult = new DiffReader(baseDiffInput, baseLeftPath,
				baseRightPath);

		Map<String, String> baseFileMap = new HashMap<String, String>();
		for (DiffResult dr : baseDiffResult.getResults()) {
			baseFileMap.put(dr.getLeftFile(), dr.getRightFile());
		}

		PsoFileListReader psoLR = new PsoFileListReader(psoInput,
				siteAbbreviation);

		Map<String, ReviseItem> reviseMap = null;

		if (reviseFile == null)
			reviseMap = new HashMap<String, ReviseItem>();
		else {
			InputStreamReader reviseFileInput = new InputStreamReader(
					Merger.class.getClassLoader().getResourceAsStream(
							reviseFile));

			ReviseReader reviseReader = new ReviseReader(reviseFileInput);
			reviseMap = reviseReader.getMap();
		}

		scan(site, siteAbbreviation, baseLeftPath, baseRightPath, baseLR,
				baseFileMap, psoLR, reviseMap);

	}

	private static void scan(String site, String siteAbbreviation,
			String baseLeftPath, String baseRightPath, FileListReader baseLR,
			Map<String, String> baseFileMap, PsoFileListReader psoLR,
			Map<String, ReviseItem> reviseMap) {
		PsoFileMatcher psoBaseFileMatcher = new PsoFileMatcher();
		psoBaseFileMatcher.match(baseLR.getItems(), psoLR.getItems());

		// list each PSO file and its counterpart
		// System.out.println("[File Match Result]");
		for (int i = 0; i < psoBaseFileMatcher.getPsoFiles().size(); i++) {
			
			
			FileItem psoFileItem = psoBaseFileMatcher.getPsoFiles().get(i);
			MatchResult mresult = psoBaseFileMatcher.getMatchResults().get(
					psoFileItem.getFullName());

			if (mresult != MatchResult.None) {

				List<String> baseOldFiles = psoBaseFileMatcher.getFileMap()
						.get(psoFileItem.getFullName());
				// if base changed
				int baseChangedCount = 0;
				List<String> baseNewFiles = new ArrayList<String>();
				for (String baseOldFile : baseOldFiles) {

					String baseNewFile = baseFileMap.get(baseOldFile);
					// if it is not null, means base changed
					if (baseNewFile == null) {

						baseNewFile = baseOldFile.replaceFirst(baseLeftPath,
								baseRightPath);

					} else {
						baseChangedCount++;
					}

					baseNewFiles.add(baseNewFile);

				}

				String baseOldFilesStr = StringUtils.join(baseOldFiles,",");
				String baseNewFilesStr = StringUtils.join(baseNewFiles,",");
				
				System.out.println(psoFileItem.getFullName() + "\t"
						+ baseOldFilesStr + "\t" + baseNewFilesStr + "\t" + (baseChangedCount>0)
						+ "\t" + mresult + "\tFalse");
			} else {

				boolean psoOnly = false;

				String shortName = psoFileItem.getShortName();

				String shortPrefix = siteAbbreviation + "PSO";
				String longPrefix = site + "PSO";
				if (shortName.length() >= shortPrefix.length()
						&& shortName.substring(0, shortPrefix.length())
								.equalsIgnoreCase(shortPrefix))
					psoOnly = true;
				else if (shortName.length() >= longPrefix.length()
						&& shortName.substring(0, longPrefix.length())
								.equalsIgnoreCase(longPrefix))
					psoOnly = true;
				else if (psoFileItem.getShortName().substring(0, 3)
						.equalsIgnoreCase("PSO"))
					psoOnly = true;

				System.out.println(psoFileItem.getFullName() + "\t\t\t\t"
						+ mresult + "\t" + psoOnly);

			}
			
			
		}
	}

}
